package XComponent.show
{
	import XComponent.hide.XPage;
	
	import flash.display.Sprite;
	import flash.events.Event;

	public class XGrid extends Sprite
	{
		private var m_xpage : XPage;
		
		private var m_hSpace : Number ;		// 水平间隔
		private var m_vSpace : Number ;		// 竖直间隔
		
		private var m_displayLst : Array;
		private var m_panLst : Array;
		
		private var m_gridWidth : Number;		//格子宽
		private var m_gridHeight : Number;		//格子高
		
		private var m_row : int = 0;			//行数
		private var m_clo : int = 0;			//列数
		
		public function XGrid(displayLst : Array,row:int=1,clo:int=1)
		{
			m_displayLst = 	displayLst;
			
			m_hSpace = 		0;
			m_vSpace = 		0;
			m_row = 		row;
			m_clo =         clo;
			
			m_xpage = 		new XPage(null,null,displayLst.length,row*clo);
			
			drawGrid();
			
			m_xpage.addEventListener(XPage.PAGE_CHANGED,OnPageChanged);
		}
		
		/**
		 *绘制格子 
		 * 
		 */		
		private function drawGrid() : void
		{
			m_panLst = [];
			
			var spaceY : int = 0;
			var spaceX : int = 0;
			
			for(var i:int=m_xpage.curBeginIndex;i<m_xpage.curEndIndex;i++)
			{
				var pan : XGridBox = new XGridBox(m_displayLst[i]);
				
				var ind : int = i%m_row;
			
				if(ind == 0 && i>0)
				{
					spaceX = 0;
					spaceY += ( pan.entity.height + m_vSpace);
				}
				
				pan.x = spaceX;
				pan.y = spaceY;
				
				spaceX += (pan.entity.width + m_hSpace);
				
				m_panLst.push(pan);
				this.addChild(pan);
			}
		}
		
		/**
		 *设置行 
		 * @param row
		 * 
		 */		
		private function set row(row : int) : void
		{
			m_row = row;
		}
		
		/**
		 *设置列 
		 * @param clo
		 * 
		 */		
		private function set clo(clo : int) : void
		{
			m_clo = clo;
		}
		
		private function OnPageChanged(evt : Event) : void
		{
			drawGrid();
		}
		
		/**
		 *重绘 
		 * 
		 */		
		public function invalidate() : void
		{
			m_xpage = 		new XPage(null,null,m_displayLst.length,m_row*m_clo);
			
			drawGrid();
		}
		
		/**
		 *清理 
		 * 
		 */		
		public function destory() : void
		{
			m_xpage.removeEventListener(XPage.PAGE_CHANGED,OnPageChanged);
			
			m_xpage.destory();
			
			m_xpage = 				null;
			
			m_hSpace =				0;
			m_vSpace = 				0;
			
			m_displayLst = 			null;
			
			m_gridHeight = 			0;
			m_gridWidth =			0;
			
			m_row = 				0;
			m_clo = 				0;
		}
	}
}